// pages/shopping/shopping.js
const AV = getApp().globalData.AV
const appdata = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        hotList: [], //精选商品列表
        changeSwitch: false, //编辑开关
        openid: '',
        data: [], // 删除id
        checked: false, //  删除
        checkeds: false, // 结算
        datas: [], // 结算id
        cartList: [], //购物车商品列表
        index: [],
        price: 0

    },
    // 跳转到主页
    goHome() {
        wx.switchTab({
            url: '/pages/home/home',
        })
    },
    // 热卖详情
    getHotDetail(e) {
        wx.navigateTo({
            url: '/pages/detail/detail?title=' + e.currentTarget.dataset.name.attributes.title + '&id=' + e.currentTarget.dataset.name.id,
        })
    },
    // 精选商品
    updateHot() {
        const query = new AV.Query('detail');
        query.greaterThanOrEqualTo("eval", '300');
        query.limit(8);
        query.find().then((hot) => {
            // console.log(hot);
            this.setData({
                hotList: hot
            })
        })
    },
    updateCart() { // 购物车列表
        const query = new AV.Query('cart')
        query.equalTo('openId', this.data.openid);
        query.equalTo('stater', "0");
        query.find().then((res) => {
            // console.log(res);
            for (let i = 0, lenI = res.length; i < lenI; ++i) {
                res[i].attributes.checked = false
                res[i].attributes.checkeds = false
            }
            this.setData({
                cartList: res,
            })
        })
    },
    checkeds() { // 全选按钮的结算
        let cartListcheckeds = this.data.cartList
        let datacheckeds = this.data.datas
        // 选择的开关
        this.setData({
            checkeds: !this.data.checkeds
        })
        // 结算时是否全选
        if (this.data.checkeds) {
            for (let index = 0; index < cartListcheckeds.length; index++) {
                cartListcheckeds[index].attributes.checkeds = true
                datacheckeds.push(cartListcheckeds[index].id)

            }
            this.setData({
                cartList: cartListcheckeds,
                datas: datacheckeds,
            })
        } else {
            // 取消全选
            for (let index = 0; index < cartListcheckeds.length; index++) {
                cartListcheckeds[index].attributes.checkeds = false
                datacheckeds = []
            }
            this.setData({
                cartList: cartListcheckeds,
                datas: datacheckeds,
            })
            this.Price()
        }
    },
    checked() { // 全选按钮的删除
        let cartListchecked = this.data.cartList
        let datachecked = this.data.data
        this.setData({
            checked: !this.data.checked
        })
        if (this.data.checked) {
            for (let index = 0; index < cartListchecked.length; index++) {
                cartListchecked[index].attributes.checked = true
                datachecked.push(cartListchecked[index].id)
            }
            this.setData({
                cartList: cartListchecked,
                data: datachecked
            })
        } else {
            for (let index = 0; index < cartListchecked.length; index++) {
                cartListchecked[index].attributes.checked = false
                datachecked = []
            }
            this.setData({
                cartList: cartListchecked,
                data: datachecked
            })
        }
    },
    dianji(e) { // 单击删除
        console.log(e);
        // 点击当前的cartList中的数据，将checked改为true
        let Data = "cartList[" + e.currentTarget.dataset.num + "].attributes.checked"
        // console.log(Data);
        this.data.data.push(e.currentTarget.dataset.name)
        this.setData({
            [Data]: true
        })
        // 判断是否全选
        this.setData({
            checked: this.data.cartList.every(item => item.attributes.checked)
        })
        // console.log(this.data.data);
    },
    dianjishi(e) { // 单击删除取消
        let Data = "cartList[" + e.currentTarget.dataset.num + "].attributes.checked"
        this.setData({
            [Data]: false
        })
        this.data.data.splice(e.currentTarget.dataset.num, 1)
        this.setData({
            checked: false
        })
        console.log(this.data.data);
    },
    dianjis(e) { // 单击结算
        // console.log(e.currentTarget.dataset.num);  //下标
        let Data = "cartList[" + e.currentTarget.dataset.num + "].attributes.checkeds"
        this.data.datas.push(e.currentTarget.dataset.name)
        this.setData({
            [Data]: true,
        })
        this.data.index.push(e.currentTarget.dataset.num)
        this.setData({
            checkeds: this.data.cartList.every(item => item.attributes.checkeds),
        })
        this.Price()
        // console.log(this.data.data);
        // console.log(this.data.datas);
    },
    dianjishis(e) { // 单击结算取消
        let Data = "cartList[" + e.currentTarget.dataset.num + "].attributes.checkeds"
        this.setData({
            [Data]: false
        })
        var over18 = this.data.index.filter(myFunction);
        console.log(this.data.index);

        function myFunction(value) {
            return value !== e.currentTarget.dataset.num;
        }
        this.setData({
            index: over18
        })
        this.data.datas.splice(e.currentTarget.dataset.num, 1)
        this.setData({
            checkeds: false,
        })
        this.Price()
        // console.log(this.data.datas);
    },
    // 控制编辑的开关，什么时候显示删除，什么时候显示结算
    changeSwitch() {
        this.setData({
            changeSwitch: !this.data.changeSwitch
        })
    },
    // 点击删除按钮删除，更新页面数据
    delete() {
        for (let i = 0; i < this.data.data.length; i++) {
            const todo = AV.Object.createWithoutData("cart", this.data.data[i]);
            todo.set("stater", "1");
            todo.save();
            todo.save().then(() => {
                wx.showToast({
                    title: '删除成功',
                })
                this.updateCart()
            });
        }
    },

    changeSize(e) {
        console.log(e.currentTarget.dataset.name);
        wx.navigateTo({
            url: '/pages/detail/detail?title=' + e.currentTarget.dataset.name,
        })
    },
    // 关闭弹窗
    closeSize() {
        this.setData({
            windowSwitch: false
        })
    },
    // 数量减少
    downfn(e) {
        e.currentTarget.dataset.id.attributes.checkeds = true
        let num = e.currentTarget.dataset.id.attributes.number
        if(num>1){
            num--
        }
        const todo = AV.Object.createWithoutData("cart", e.currentTarget.dataset.id.id);
        todo.set("number", num);
        todo.save().then(() => {
            this.updateCart()
            this.Price()
            this.updata(e.currentTarget.dataset.index, e.currentTarget.dataset.id)
        });
    },

    // 下标,控制是否被选中时的状态
    updata(indexs, list) {
        // console.log(indexs);
        // console.log(list);
        const query = new AV.Query('cart')
        query.equalTo('openId', this.data.openid);
        query.equalTo('stater', "0");
        query.find().then((res) => {
            if (list.attributes.checkeds) {
                for (let i = 0, lenI = res.length; i < lenI; ++i) {
                    res[i].attributes.checkeds = false
                }
                this.data.index.push(indexs)
                console.log(this.data.index);
                for (let index = 0; index < this.data.index.length; ++index) {
                    res[this.data.index[index]].attributes.checkeds = true
                }
                this.setData({
                    cartList: res
                })
                this.Price()
            }
        })
    },
    // 数量增加
    addfn(e) {
        e.currentTarget.dataset.id.attributes.checkeds = true
        // console.log(e.currentTarget.dataset.id);
        let num = e.currentTarget.dataset.id.attributes.number
        num++
        const todo = AV.Object.createWithoutData("cart", e.currentTarget.dataset.id.id);
        todo.set("number", num);
        todo.save().then(() => {
            this.updateCart()
            this.Price()
            this.updata(e.currentTarget.dataset.index, e.currentTarget.dataset.id)
        });
    },
    Price() {
        //获取商品列表数据
        let shopData = this.data.cartList
        //声明一个变量接收数组列表price
        let total = 0
        for (let i = 0, len = shopData.length; i < len; i++) {
            //判断选中才会计算价格
            if (shopData[i].attributes.checkeds) {
                total += shopData[i].attributes.number * shopData[i].attributes.price;
            }
        }
        // 最后赋值到data中渲染到页面
        this.setData({
            cartList: shopData,
            price: total
        });
    },
    // 结算按钮
    total() {
        // console.log(this.data.datas);
        if (this.data.datas.length >= 1) {
            wx.navigateTo({
                url: '/pages/order/order?num=0&id=' + this.data.datas,
            })
            this.setData({
                checkeds: false
            })
            this.Price()

        }


    },
    onLoad: function (options) {
        wx.showLoading({
            title: '正在加载中',
        })
        setTimeout(function () {
            wx.hideLoading()
        }, 1000)
    },
    onShow: function () {
        const openids = appdata.globalData.userInfo
        this.setData({
            openid: openids
        })
        this.updateCart()
        this.updateHot()
        this.Price()

    }
})